# -*- coding: utf-8 -*-


from keras import Model
import keras.backend as K
from keras.layers import Embedding,Reshape,Input,Dot
import pandas as pd
import numpy as np
K.clear_session()

# 模型搭建
def Recommend_model(num_user,num_movie,k):
    input_uer = Input(shape=[None,],dtype="int32")
    model_uer = Embedding(num_user+1,k,input_length = 1)(input_uer)
    model_uer = Reshape((k,))(model_uer)
    
    input_movie = Input(shape=[None,],dtype="int32")
    model_movie  = Embedding(num_movie+1,k,input_length = 1)(input_movie)
    model_movie = Reshape((k,))(model_movie)
    
    out = Dot(1)([model_uer,model_movie])
    model = Model(inputs=[input_uer,input_movie], outputs=out)
    model.compile(loss='mse', optimizer='Adam')
    model.summary()
    return model


rating = pd.read_csv("data/ml-latest-small/ratings.csv",sep=",")
num_user = np.max(rating["userId"])
num_movie = np.max(rating["movieId"])
print("num_user = %s, num_movie = %s rating = %s" % (num_user,num_movie,len(rating)))
model = Recommend_model(num_user, num_movie, 100) # 100 个神经元

# 数据准备
train_user = rating["userId"].values
train_movie = rating["movieId"].values
train_x = [train_user,train_movie]
train_y = rating["rating"].values
# 模型训练
model.fit(train_x,train_y,batch_size = 100,epochs =10)
# 模型预测
model.predict([[1],[2]])

# num_user = 610, num_movie = 193609 rating = 100836
# Model: "model"
# __________________________________________________________________________________________________
# Layer (type)                    Output Shape         Param #     Connected to                     
# ==================================================================================================
# input_1 (InputLayer)            [(None, None)]       0                                            
# __________________________________________________________________________________________________
# input_2 (InputLayer)            [(None, None)]       0                                            
# __________________________________________________________________________________________________
# embedding (Embedding)           (None, None, 100)    61100       input_1[0][0]                    
# __________________________________________________________________________________________________
# embedding_1 (Embedding)         (None, None, 100)    19361000    input_2[0][0]                    
# __________________________________________________________________________________________________
# reshape (Reshape)               (None, 100)          0           embedding[0][0]                  
# __________________________________________________________________________________________________
# reshape_1 (Reshape)             (None, 100)          0           embedding_1[0][0]                
# __________________________________________________________________________________________________
# dot (Dot)                       (None, 1)            0           reshape[0][0]                    
#                                                                  reshape_1[0][0]                  
# ==================================================================================================
# Total params: 19,422,100
# Trainable params: 19,422,100
# Non-trainable params: 0
# __________________________________________________________________________________________________